package rabbit.one;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created with IntelliJ IDEA.
 * ClassName: Custom
 * Package: rabbit.one
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-10-25
 * Time: 14:47
 */

//消费者

public class Customer {

    //队列的名称 和 生产者的一样
    public static final  String QUEUE_NAME = "hello";
    //接收消息
    public static void main(String[] args) throws IOException, TimeoutException {
        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("hadoop100");
        factory.setUsername("admin");
        factory.setPassword("123");
        //创建连接
        Connection connection = factory.newConnection();
        //连接信道
        Channel channel = connection.createChannel();
        //通过信道接收消息
        /**
         * 1.消费那个队列
         * 2.消费成功之后是否要自动应答 true 代表自动应答 false 代表手动应答
         * 3.消费者接收消息之后的回调
         * 4.消费者取消消费的回调函数
         */
        //接收消息
        DeliverCallback deliverCallback = (consumerTag,message) ->{
            System.out.println(new String(message.getBody()));
        };
        //取消消息的回调
        CancelCallback cancelCallback = consumerTag ->{
            System.out.println("消息中断被回调");
        };
        channel.basicConsume(QUEUE_NAME,true,deliverCallback,cancelCallback);

    }

}
